package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.GouMaiKeChengActivity;
import com.xgkj.diyiketang.activity.GuoxueVideoTwoActivity;
import com.xgkj.diyiketang.activity.VideoDetailActivity;
import com.xgkj.diyiketang.activity.ZhiboPasswordActivity;
import com.xgkj.diyiketang.data.VideoInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.yizhibo.Constants;
import com.xgkj.diyiketang.yizhibo.PlayerNewActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrater on 2015/12/8.
 */
public class VideoAdapter extends BaseAdapter {
    private Context mContext;
    private List<VideoInfo> mList = new ArrayList<VideoInfo>();
    private int isCharge = 1;

    public VideoAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<VideoInfo> list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mList != null) {
            mList.clear();
        }

        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mList != null && mList.size() > 0) {
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mList != null && mList.size() > 0) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChildHolder childHolder = null;
        if (convertView == null) {
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.video_item, null);
            childHolder.videoImage = (ImageView) convertView.findViewById(R.id.video_img);
            childHolder.videoTime = (TextView) convertView.findViewById(R.id.video_time);
            childHolder.videoTitle = (TextView) convertView.findViewById(R.id.video_title);
            childHolder.videoInstruction = (TextView) convertView.findViewById(R.id.video_instruction);
            childHolder.viewTypeImage = (ImageView) convertView.findViewById(R.id.viewtype);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }

        final VideoInfo info = mList.get(position);
//        info.setVideoTitle("aaa");
//        info.setVideoIntroduction("dafadfasf");
//        info.setVideoDuration("12:00");
//        info.setVideoLogo("http://pic.nipic.com/2007-11-09/2007119122519868_2.jpg");
        //(VideoInfo) getItem(position);
        if (info != null) {
            String vidoeIconUrl = info.getVideoLogo();
            if (vidoeIconUrl != null && vidoeIconUrl.length() > 0) {
                Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(vidoeIconUrl);
                if (cacheBitmap != null) {
                    childHolder.videoImage.setImageBitmap(cacheBitmap);
                    childHolder.videoImage.setScaleType(ImageView.ScaleType.FIT_CENTER);
                } else {
                    childHolder.videoImage.setImageResource(R.drawable.zhanweitu);
                    ImageCache.getInstance(mContext).loadBitmapToView(childHolder.videoImage, vidoeIconUrl, new ViewUtil.Size(ViewUtil.dip2px(mContext, 170), ViewUtil.dip2px(mContext, 100)));
                }
            }
            childHolder.videoTime.setText(info.getVideoDuration());
            childHolder.videoTitle.setText(info.getVideoTitle());
            childHolder.videoInstruction.setText(info.getVideoIntroduction());
            childHolder.viewTypeImage.setVisibility(View.VISIBLE);
            int viewType = info.getViewType();
            if (viewType == 1) {
                childHolder.viewTypeImage.setImageResource(R.drawable.vip);
            } else if (viewType == 2) {
                childHolder.viewTypeImage.setImageResource(R.drawable.shoufei);
            } else {
                childHolder.viewTypeImage.setVisibility(View.GONE);
            }

            childHolder.videoImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String uId = info.getUserId();
                    String videoId = info.getVideoId();
                    String youkuVideoId = info.getPath();
                    String title = info.getVideoTitle();
                    String zuozhe = info.getUserName();
                    String fee = info.getFee();
                    String headUrl = info.getUserLogo();
                    String anchor = info.getUserName();
                    String isuse = info.getIsuse();
                    String isPlayer = info.getIsMoive2();
                    String isMovie = info.getIsMoive();
                    int liveType = info.getViewType();
                    if (null != info.getIsCharge()) {
                        isCharge = Integer.parseInt(info.getIsCharge());
                    }
                    int finalVip = 1;
                    String vid = info.getVid();
//                    int typeView = info.getViewType();
//                    if(typeView ==2){
//                        Intent gmIntent = new Intent(mContext, GouMaiKeChengActivity.class);
//                        gmIntent.putExtra("ZUOZHE",zuozhe);
//                        gmIntent.putExtra("FEE",fee);
//                        gmIntent.putExtra("TITLE",title);
//                        gmIntent.putExtra("VDID",videoId);
//                        mContext.startActivity(gmIntent);
//                    }else {
                    if ("0".equals(info.getType())) {
//                        Intent intent = new Intent(mContext, VideoDetailActivity.class);
                        Intent intent = null;
                        if (!TextUtils.isEmpty(info.getIsMoive())){

                            if (isMovie.equals("0")){
                                intent = new Intent(mContext, VideoDetailActivity.class);
                            }else {
                                intent = new Intent(mContext, GuoxueVideoTwoActivity.class);
                            }
                            intent.putExtra("isMovie",isMovie);
                        }
//                        Intent intent = new Intent(mContext, GuoxueVideoTwoActivity.class);
                        intent.putExtra("videoid", videoId);
                        intent.putExtra("UID", uId);
                        intent.putExtra("isUse",isuse);
                        intent.putExtra("YOUKUVIDEOID", youkuVideoId);
                        intent.putExtra("ZUOZHE", zuozhe);
                        intent.putExtra("FEE", fee);
                        intent.putExtra("TITLE", title);
                        mContext.startActivity(intent);
                    } else {
                        if (liveType == 0 || (liveType == 1 && isCharge == 1)) {
                            Intent intent = new Intent(mContext, PlayerNewActivity.class);
                            intent.putExtra("videoid", videoId);
                            intent.putExtra("isMovie",isMovie);
                            intent.putExtra("isUse",isuse);
                            intent.putExtra(Constants.ZHIBO_PLAY,isPlayer);
                            intent.putExtra(Constants.EXTRA_KEY_VIDEO_ID, vid);
//                            intent.putExtra(Constants.LIVE_STATE, liveState);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            mContext.startActivity(intent);
                        } else if (liveType == 1 && finalVip != 0) {
                            //收费
                            Intent intent = new Intent(mContext, GouMaiKeChengActivity.class);
                            intent.putExtra("From", 1);
                            intent.putExtra("TITLE", title);
                            intent.putExtra("ZUOZHE", anchor);
                            intent.putExtra("FEE", fee);
                            intent.putExtra(Constants.EXTRA_KEY_VIDEO_ID, vid);

//                        intent.putExtra("ZHIBO_LIST", getDataList());
//                        intent.putExtra("CURRENT_POSITION", currentPosition);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            mContext.startActivity(intent);
                        } else if (liveType == 2 && isCharge == 0) {
                            //收费
                            Intent intent = new Intent(mContext, GouMaiKeChengActivity.class);
                            intent.putExtra("From", 1);
                            intent.putExtra("TITLE", title);
                            intent.putExtra("ZUOZHE", anchor);
                            intent.putExtra("FEE", fee);
                            intent.putExtra(Constants.EXTRA_KEY_VIDEO_ID, vid);
//                        intent.putExtra("ZHIBO_LIST", getDataList());
//                        intent.putExtra("CURRENT_POSITION", currentPosition);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            mContext.startActivity(intent);
                        } else if (liveType == 8) {
                            //密码
                            Intent intent = new Intent(mContext, ZhiboPasswordActivity.class);
                            intent.putExtra("head_url", headUrl);
                            intent.putExtra("nickname", anchor);
                            intent.putExtra(Constants.EXTRA_KEY_VIDEO_ID, vid);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            mContext.startActivity(intent);
//                        intent.putExtra("ZHIBO_LIST", getDataList());
//                        intent.putExtra("CURRENT_POSITION", currentPosition);
                        } else if (liveType == 3 && finalVip != 0) {
                            Intent intent = new Intent(mContext, PlayerNewActivity.class);
                            intent.putExtra("isUse",isuse);
                            intent.putExtra("videoid", videoId);
                            intent.putExtra(Constants.EXTRA_KEY_VIDEO_ID, vid);
//                        intent.putExtra("ZHIBO_LIST", getDataList());
//                        intent.putExtra("CURRENT_POSITION", currentPosition);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            mContext.startActivity(intent);
                        } else if (liveType == 3 && finalVip == 0) {
                            Toast.makeText(mContext, "请购买VIP后观看", Toast.LENGTH_SHORT).show();
                        }
                    }
//                    }


                }
            });
        }
        return convertView;
    }

    class ChildHolder {
        ImageView videoImage;
        ImageView viewTypeImage;
        TextView videoTime;
        TextView videoTitle;
        TextView videoInstruction;
    }


}
